#!/usr/bin/Rscript

block = as.matrix(read.table('fdl-pan-evap-inch.txt',fill=T,header=F))

year = 63
mon = 3

listyear = 1963

con = file("fdl-pan-evap-inch.formatted.txt",'w')

for(i in 1:length(unique(block[,1]))){

	mon = mon + 1
	if( mon > 12 ){
        mon = 1
        year = year + 1
        listyear = listyear + 1
        if( year > 99 ) year = 0
    }
	

	monthdata = as.vector(t(block[block[,1]==(mon*100+year),][,2:7]))
	monthdata = monthdata[!is.na(monthdata)]
    
    ndays = length(monthdata)

	monthdata = cbind(rep(listyear,ndays),rep(mon,ndays),seq(1,ndays),monthdata)
    if(mon == 12 | mon == 1 | mon == 2){
        write(t(monthdata),file=con,ncolumns=4,append=T) 
        #print(monthdata) 
    }
}

close(con)
###
